<div class="row">
    <div class="col-lg-9">
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <form method="post" id="form-list" action="index.php?r=navgation">
                        <input type="hidden" name="action_type" value="delete">
                        <input type="hidden" name="navgation_id">
                        <table class="table">
                            <thead>
                            <tr>
                                <th width="20%">导航</th>
                                <th width="20%">类型</th>
                                <th width="20%">排序</th>
                                <th width="20%">图标样式</th>
                                <th width="20%">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            foreach ($navgations as $navgation) {
                                ?>
                                <tr>
                                    <td><?php echo $navgation['navgation_text']; ?></td>
                                    <td><?php echo ConstantVar::$navgationType[$navgation['navgation_type']]; ?></td>
                                    <td><?php echo $navgation['navgation_order']; ?></td>
                                    <td><?php echo $navgation['navgation_class']; ?></td>
                                    <td>
                                        <button type="button" onclick="edit(<?php echo $navgation['navgation_id']; ?>)" class="btn btn-primary btn-sm m-l-5">编辑</button>
                                        <button type="button" onclick="del(<?php echo $navgation['navgation_id']; ?>)"
                                                class="btn btn-danger btn-sm m-l-5">删除
                                        </button>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-3">
        <?php
        if (isset($editNavgation)) {
            ?>
            <div class="card">
                <div class="card-title">
                    编辑菜单
                </div>
                <div class="card-body">
                    <form method="post" id="form-edit" action="index.php?r=navgation">
                        <input type="hidden" name="action_type" value="updatedo">
                        <input type="hidden" name="navgation_id" value="<?php echo $editNavgation['navgation_id'] ?>"/>
                        <div class="form-group">
                            <label>菜单</label>
                            <input type="text" class="form-control" name="navgation_text" value="<?php echo $editNavgation['navgation_text'] ?>" minlength="1"
                                   placeholder="菜单">
                        </div>
                        <div class="form-group">
                            <label>菜单排序</label>
                            <input type="text" class="form-control" name="navgation_order" value="<?php echo $editNavgation['navgation_order'] ?>" minlength="1"
                                   placeholder="菜单排序">
                        </div>
                        <div class="form-group">
                            <label>图标样式</label>
                            <input type="text" class="form-control" name="navgation_class" value="<?php echo $editNavgation['navgation_class'] ?>" minlength="1"
                                   placeholder="图标样式">
                        </div>
                        <div class="form-group">
                            <label>类型</label>
                            <select class="form-control" name="navgation_type" onchange="changeNavgationType()">
                                <option value="link" <?php echo $editNavgation['navgation_type']=='link'?"selected":""; ?> >链接</option>
                                <option value="article" <?php echo $editNavgation['navgation_type']=='article'?"selected":""; ?>>文章</option>
                                <option value="image" <?php echo $editNavgation['navgation_type']=='image'?"selected":""; ?>>图像</option>
                                <option value="file" <?php echo $editNavgation['navgation_type']=='file'?"selected":""; ?>>文件</option>
                            </select>
                        </div>
                        <div class="form-group navgation_type" id="select-link">
                            <label>链接地址</label>
                            <input type="text" class="form-control" value="<?php echo isset($editNavgationMeta['link_address'])?$editNavgationMeta['link_address']:""; ?>" name="link_address" minlength="1"
                                   placeholder="链接地址">
                        </div>
                        <div class="form-group navgation_type" id="select-article">
                            <label>文章分类</label>
                            <select class="form-control" name="type_article">
                                <?php
                                foreach ($categoryOfArticle as $item) {
                                    if(isset($editNavgationMeta['category_id'])&& $editNavgationMeta['category_id'] == $item['category_id'] ){
                                        ?>
                                        <option selected="selected" value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                        <?php
                                    }else{
                                        ?>
                                        <option value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                        <?php
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <div class="form-group navgation_type" id="select-image">
                            <label>图像分类</label>
                            <select class="form-control" name="type_image">
                                <?php
                                foreach ($categoryOfImage as $item) {
                                    if(isset($editNavgationMeta['category_id']) && $editNavgationMeta['category_id'] == $item['category_id'] ){
                                        ?>
                                        <option selected="selected" value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                        <?php
                                    }else{
                                        ?>
                                        <option value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                        <?php
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <div class="form-group navgation_type" id="select-file">
                            <label>文件分类</label>
                            <select class="form-control" name="type_file">
                                <?php
                                foreach ($categoryOfFile as $item) {
                                    if(isset($editNavgationMeta['category_id']) && $editNavgationMeta['category_id'] == $item['category_id'] ){
                                        ?>
                                        <option selected="selected" value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                        <?php
                                    }else{
                                        ?>
                                        <option value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                        <?php
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">保存</button>
                    </form>
                </div>
            </div>
            <?php
        } else {
            ?>
            <div class="card">
                <div class="card-title">
                    新增菜单
                </div>
                <div class="card-body">
                    <form method="post" id="form-edit" action="index.php?r=navgation">
                        <input type="hidden" name="action_type" value="insert">
                        <div class="form-group">
                            <label>菜单</label>
                            <input type="text" class="form-control" name="navgation_text" minlength="1"
                                   placeholder="菜单">
                        </div>
                        <div class="form-group">
                            <label>菜单排序</label>
                            <input type="text" class="form-control" name="navgation_order" minlength="1"
                                   placeholder="菜单排序">
                        </div>
                        <div class="form-group">
                            <label>图标样式</label>
                            <input type="text" class="form-control" name="navgation_class" minlength="1"
                                   placeholder="图标样式">
                        </div>
                        <div class="form-group">
                            <label>类型</label>
                            <select class="form-control" name="navgation_type" onchange="changeNavgationType()">
                                <option value="link">链接</option>
                                <option value="article">文章</option>
                                <option value="image">图像</option>
                                <option value="file">文件</option>
                            </select>
                        </div>
                        <div class="form-group navgation_type" id="select-link">
                            <label>链接地址</label>
                            <input type="text" class="form-control" name="link_address" minlength="1"
                                   placeholder="链接地址">
                        </div>
                        <div class="form-group navgation_type" id="select-article">
                            <label>文章分类</label>
                            <select class="form-control" name="type_article">
                                <?php
                                foreach ($categoryOfArticle as $item) {
                                    ?>
                                    <option value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div class="form-group navgation_type" id="select-image">
                            <label>图像分类</label>
                            <select class="form-control" name="type_image">
                                <?php
                                foreach ($categoryOfImage as $item) {
                                    ?>
                                    <option value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div class="form-group navgation_type" id="select-file">
                            <label>文件分类</label>
                            <select class="form-control" name="type_file">
                                <?php
                                foreach ($categoryOfFile as $item) {
                                    ?>
                                    <option value="<?php echo $item['category_id'] ?>"><?php echo $item['categoryname'] ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">保存</button>
                    </form>
                </div>
            </div>
            <?php
        }
        ?>

    </div>
    <!-- /# column -->
</div>
<!-- /# row -->
<script>
    $(function () {
        changeNavgationType();
    });

    function changeNavgationType() {
        var navgation_type = $("select[name=navgation_type]").val();
        $(".navgation_type").hide();
        $("#select-" + navgation_type).show();
    }

    function del(navgation_id) {
        $("#form-list").find("input[name=navgation_id]").val(navgation_id);
        $("#form-list").find("input[name=action_type]").val("delete");
        $("#form-list").submit();
    }

    function edit(navgation_id) {
        $("#form-list").find("input[name=navgation_id]").val(navgation_id);
        $("#form-list").find("input[name=action_type]").val("update");
        $("#form-list").submit();
    }
</script>